package com.avic.process.utils;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.CellEditor;
import javax.swing.JTextArea;
import javax.swing.event.ChangeEvent;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import com.teamcenter.rac.ecmanagement.sorttable.JSortTable;
import com.teamcenter.rac.ecmanagement.sorttable.SortTableModel;
import com.teamcenter.rac.util.iPopupMenu;

public class CascJSortTable extends JSortTable implements FocusListener
{
    /**
     * 
     */
    private iPopupMenu ipm = null;
    private int MLineColumns[];
    private boolean is_MLine = false;
    private static final long serialVersionUID = 1L;

    private int pmRow = -1;
    private int pmColumn = -1;

    private double mlinescal = 2.6;

    private int ColumnsWidths[] = null;
 
    public CascJSortTable()
    {
    	//addFocusListener(this);
    }
    
    public void setMLineColumns(int i[])
    {
        if(i != null && i.length > 0)
        {
            is_MLine = true;
            MLineColumns = i;
        }

    }
    
    /*public void FocusLost(FocusEvent evt) {                                    
        // TODO add your handling code here:  
    	
    	//getCellEditor().removeCellEditorListener(l)
         CellEditor editor= getCellEditor();  
         
        System.out.println("zhangsan......");
    } */
    public void setMLineScale(double d)
    {
        if(d > 0.98999999)
        {
            mlinescal = d;
        }

    }

    public void mouseReleased(MouseEvent mouseevent)
    {
    	
    }
   /* public void mouseClicked(MouseEvent mouseevent)
    {
        if(mouseevent.getButton() == MouseEvent.BUTTON3)
            return;
      //  System.out.println("zhangsan");
        TableColumnModel tablecolumnmodel = getColumnModel();
        int i = tablecolumnmodel.getColumnIndexAtX(mouseevent.getX());
        int j = tablecolumnmodel.getColumn(i).getModelIndex();
        CPFGCascTableModel sorttablemodel = (CPFGCascTableModel)getModel();
        if(sorttablemodel.isSortable(j))
        {
            if(sortedColumnIndex == i)
                sortedColumnAscending = !sortedColumnAscending;
            sortedColumnIndex = i;
            sorttablemodel.sortColumn(j, sortedColumnAscending);
        }
    }*/

    public void setPopupMenu(iPopupMenu i)
    {
        ipm = i;
    }

    public void showPopupMenu(Component component, int i, int j)
    {
        Point point = new Point(i, j);

        pmRow = rowAtPoint(point);
        pmColumn = columnAtPoint(point);
        if(ipm != null)
        {
            ipm.show(component, i, j);
        }
    }

    public int getPopupMenuRow()
    {
        return pmRow;
    }

    public int getPopupMenuColumn()
    {
        return pmColumn;
    }

    public void columnMarginChanged(ChangeEvent changeevent)
    {

        if(isEditing())
            removeEditor();
        TableColumn tablecolumn = getTableHeader().getResizingColumn();
        if(tablecolumn != null && autoResizeMode == 0)
            tablecolumn.setPreferredWidth(tablecolumn.getWidth());
        resizeAndRepaint();

        columnChanged();
    }

    public void columnChanged()
    {
        int h = 16;
        int i, j;
        int height = h;
        String s;

        // System.out.println("columnMarginChanged");
        if(is_MLine)
        {
            JTextArea jta = new JTextArea();
            jta.setLineWrap(true);
            jta.setWrapStyleWord(true);

            TableColumnModel cm = getColumnModel();

            for(i = 0; i < getRowCount(); i++)
            {
                h = 20;
                for(j = 0; j < MLineColumns.length; j++)
                {
                    s = "" + getValueAt(i, MLineColumns[j]);
                    if(s.length() > 0)
                    {
                        jta.setSize(cm.getColumn(MLineColumns[j]).getWidth(), 1);
                        jta.setText(s);
                        height = (int)(jta.getPreferredSize().height * mlinescal);
                        if(height > h)
                        {
                            h = height;
                        }
                    }

                }
                setRowHeight(i, h);
            }
        }
    }

    public void setColumnsWidths(int[] ws)
    {
        ColumnsWidths = ws;
    }

    public void doLayout()
    {
        int sub;
        int w;
        int n;
        int i;
        if(ColumnsWidths != null && ColumnsWidths.length > 0 && columnModel != null)
        {
            ArrayList<TableColumn> tcs = new ArrayList<TableColumn>();
            ArrayList<Integer> is = new ArrayList<Integer>();

            n = columnModel.getColumnCount();
            for(i = 0; i < n; i++)
            {
                if(i < ColumnsWidths.length && ColumnsWidths[i] > 0)
                {
                    columnModel.getColumn(i).setWidth(ColumnsWidths[i]);
                }else
                {
                    tcs.add(columnModel.getColumn(i));
                    is.add(columnModel.getColumn(i).getWidth());
                }
            }
            n = tcs.size();
            if(n > 0)
            {
                sub = getWidth() - columnModel.getTotalColumnWidth();
                w = sub / n;
                for(i = 0; i < n - 1; i++)
                {
                    tcs.get(i).setWidth(is.get(i) + w);
                }
                tcs.get(n - 1).setWidth(is.get(n - 1) + (sub - w * n +w));
            }

        }else
        {
            super.doLayout();
        }
    }

	@Override
	public void focusGained(FocusEvent e) {
		// TODO Auto-generated method stub
		//System.out.println("focusGained");
	}

	@Override
	public void focusLost(FocusEvent e) {
		// TODO Auto-generated method stub
		//System.out.println("focusLost");
		 // TODO add your handling code here:  
        CellEditor editor= getCellEditor();  
       if(isEditing()&&editor!=null)  
       {  
    	   System.out.println("zhang");
          boolean b= editor.stopCellEditing();  
          if(!b)  
          {  
              editor.cancelCellEditing();  
          }  
       } 
	}

}
